{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from math import *\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "interval = 0.001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3x3 memristor crossbar\n",
    "# 전압으로 MEMRISTOR 저\n",
    "class Memristor:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "        self.x = 0.1\n",
    "        self.xp = 0.1\n",
    "        self.xn = 0.2\n",
    "        self.vp = 1.5\n",
    "        self.vn = 0.7\n",
    "        self.alphap = 6\n",
    "        self.alphan = 4\n",
    "        self.a1 = 4e-4\n",
    "        self.a2 = 3e-4\n",
    "        self.ap = 0.032\n",
    "        self.an = 0.001\n",
    "        self.b = 1.0\n",
    "        \n",
    "        self.gf = 0.0\n",
    "        \n",
    "        self.interval = interval\n",
    "    \n",
    "    def change_x(self, V):\n",
    "        if V >= 0:\n",
    "            if self.x >= self.xp:\n",
    "                self.f = exp(-self.alphap*(self.x - self.xp)) * (((self.xp - self.x)/(1 - self.xp)) + 1)\n",
    "            else:\n",
    "                self.f = 1.0\n",
    "        elif V < 0:\n",
    "            if self.x <= (1 - self.xn):\n",
    "                self.f = exp(self.alphan*(self.x + self.xn - 1.0)) * (self.x/(1-self.xn))\n",
    "            else:\n",
    "                self.f = 1.0\n",
    "            \n",
    "        if V > self.vp:\n",
    "            self.g = self.ap * (exp(V)-exp(self.vp))\n",
    "        elif V < -self.vn:\n",
    "            self.g = -self.an * (exp(-V)-exp(self.vn))\n",
    "        else:\n",
    "            self.g = 0\n",
    "            \n",
    "        self.gf = self.f * self.g\n",
    "        self.x += self.gf * self.interval\n",
    "    \n",
    "    def activate(self, V):\n",
    "        self.change_x(V)\n",
    "        \n",
    "        if V >= 0:\n",
    "            curr = self.a1 * self.x * sinh(self.b * V)\n",
    "        else:\n",
    "            curr = self.a2 * self.x * sinh(self.b * V)\n",
    "        \n",
    "        self.resistance = V / curr\n",
    "        \n",
    "        return curr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# weight mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "w = np.array([[0.5,0.2,0.5],\n",
    "             [0.5,0.2,0.5],\n",
    "             [1.0,0.2,1.0],\n",
    "             [0.5,0.5,1.0],\n",
    "             [0.2,0.2,0.2],\n",
    "             [1.0,1.0,0.2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "w = w.reshape(9,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.5 0.2]\n",
      " [0.5 0.5]\n",
      " [0.2 0.5]\n",
      " [1.  0.2]\n",
      " [1.  0.5]\n",
      " [0.5 1. ]\n",
      " [0.2 0.2]\n",
      " [0.2 1. ]\n",
      " [1.  0.2]]\n"
     ]
    }
   ],
   "source": [
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x261f63410b8>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOUAAAOfCAYAAAA3mAQsAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAEu5JREFUeJzt212MpndZx/Hrcmf7Kg1SK0jbCEZo0hBez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Zj5eVX0AW4Dyjh6II0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oII0oIs7XERV/44gfqxhtvXeLSnKFXPfewF/BdX56TG51zp4QwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwooQwe0bZ3ed09z929+e6+/bu/oODGAZrtbXBmYeq6hUz853uPlpVH+/uv5uZTy28DVZpzyhnZqrqO7sfHt39NUuOgjXb6GfK7j7S3bdW1b1V9ZGZuXnZWbBeG0U5M6dm5qVVdUlVXdHdL3rime4+1t073b1z3/2n9nsnrMYZvfo6M/9RVTdV1dWn+drxmdmeme2LLjyyT/NgfTZ59fWi7n7m7u/PrapXVtUXlh4Ga7XJq68/WlV/2d1H6rGI3z0zH1h2FqzXJq++/lNVvewAtgDlHT0QR5QQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQRpQQZuuwB7Cse6678rAnsOuR6z+10Tl3SggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSggjSgizcZTdfaS7P9vdH1hyEKzdmdwp31hVdyw1BHjMRlF29yVV9UtV9fZl5wCb3infWlVvqqr/fqoD3X2su3e6e+e++0/tyzhYoz2j7O5frqp7Z+Yz/9e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BGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBGlBBmoyi7+5nd/Z7u/kJ339HdP7P0MFirrQ3P/UlVfWhmfqW7z6qq8xbcBKu2Z5TdfUFV/WxV/XpV1cw8XFUPLzsL1muTx9cfr6r7quovuvuz3f327j7/iYe6+1h373T3ziP10L4PhbXYJMqtqvrJqvqzmXlZVZ2sqjc/8dDMHJ+Z7ZnZPlpn7/NMWI9NojxRVSdm5ubdj99Tj0UKLGDPKGfmnqr6andftvupq6rq84uughXb9NXX36qq63dfeb2rqn5juUmwbhtFOTO3VtX2wluA8o4eiCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCCNKCLN12ANY1o1333rYE9h1xase2OicOyWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWE2TPK7r6su2993K9vdfd1BzEO1mhrrwMz88WqemlVVXcfqaqvVdX7Ft4Fq3Wmj69XVdW/zsyXlxgDbHCnfIJrquqG032hu49V1bGqqnPqvKc5C9Zr4ztld59VVa+pqr853ddn5vjMbM/M9tE6e7/2weqcyePrq6vqlpn596XGAGcW5bX1FI+uwP7ZKMruPq+qfr6q3rvsHGCjF3pm5oGqunDhLUB5Rw/EESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWEESWE2Vrioi988QN14423LnFpztCrnvvSw57Arn+Z+zc6504JYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYUQJYTaKsrt/p7tv7+7buvuG7j5n6WGwVntG2d0XV9VvV9X2zLyoqo5U1TVLD4O12vTxdauqzu3urao6r6ruXm4SrNueUc7M16rqj6rqK1X19ar6z5n58NLDYK02eXz9oap6bVU9v6qeW1Xnd/frTnPuWHfvdPfOffef2v+lsBKbPL6+sqr+bWbum5lHquq9VXXlEw/NzPGZ2Z6Z7YsuPLLfO2E1NonyK1X10919Xnd3VV1VVXcsOwvWa5OfKW+uqvdU1S1V9c+7/87xhXfBam1tcmhm3lJVb1l4C1De0QNxRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhRAlhemb2/6Ld91XVl/f9wgfrh6vqG4c9gqr6/vle/NjMXLTXoUWi/H7Q3Tszs33YO1jf98LjK4QRJYQR5VM7ftgD+F+r+l74mRLCuFNCGFFCGFFCGFFCGFFCmP8Bka9dVCmVez8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x261f3705668>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.matshow(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Building memristor crossbar architecture\n",
    "for i in range(len(w)):\n",
    "    for j in range(len(w[0])):\n",
    "        exec('mem_%d%d = Memristor(\"mem_%d%d\")'%(i,j,i,j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Mapping on the crossbar\n",
    "def mapping(w):\n",
    "    V = 1.7\n",
    "    output = []\n",
    "    for i, row in enumerate(w):\n",
    "        out_row = []\n",
    "        for j, t  in enumerate(row):\n",
    "            for time in np.arange(0,t,interval):\n",
    "                exec('mem_%d%d.activate(V)'%(i,j))\n",
    "            \n",
    "            exec('out_row.append(mem_%d%d.x)'%(i,j))\n",
    "        output.append(out_row)\n",
    "    \n",
    "    return np.array(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = mapping(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.115043   0.10621186]\n",
      " [0.115043   0.115043  ]\n",
      " [0.10621186 0.115043  ]\n",
      " [0.12862764 0.10621186]\n",
      " [0.12862764 0.115043  ]\n",
      " [0.115043   0.12862764]\n",
      " [0.10621186 0.10621186]\n",
      " [0.10621186 0.12862764]\n",
      " [0.12862764 0.10621186]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x261f63c10f0>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOUAAAOfCAYAAAA3mAQsAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIu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IYUUIYUUIYUUIYUUIYUUIYUUIYUUKYnpm9v2j3vVX15T2/8P76war6+kGPoKq+d74XPzYzl+x2aJEovxd097GZ2T7oHazve+HxFcKIEsKI8skdPegB/K9VfS/8TAlh3CkhjCghjCghjCghjCghzP8A+cRdT2B8fFUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x261f64c8d30>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(a)\n",
    "plt.matshow(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array([[1,0,1],[0,1,0],[1,0,1]])\n",
    "T = np.array([[1,1,1],[0,1,0],[0,1,0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 1]\n",
      " [0 1 0]\n",
      " [1 0 1]] \n",
      "\n",
      "[[1 1 1]\n",
      " [0 1 0]\n",
      " [0 1 0]]\n"
     ]
    }
   ],
   "source": [
    "print(X, '\\n')\n",
    "print(T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = X.reshape(1,9)\n",
    "T = T.reshape(1,9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2.9 1.6]] [[2.4 2.7]]\n"
     ]
    }
   ],
   "source": [
    "output1 = np.dot(X,w)\n",
    "output2 = np.dot(T,w)\n",
    "\n",
    "print(output1, output2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# After-imaging method"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 우선은 회로도에 대한 고려없이 해보았습니다"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import mnist data set and do max_pooling or avg_pooling or big stride Convolution to reduce the size of the image. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Build Memristor crossbar\n",
    "\n",
    "row_num = 9 # input image data size = 3*3\n",
    "column_num = 2 # class number\n",
    "for i in range(row_num):\n",
    "    for j in range(column_num):\n",
    "        exec('mem_%d%d = Memristor(\"mem_%d%d\")'%(i,j,i,j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 아래 내용을 하나의 class로 만들어도 괜찮을 것 같다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def crossbar_animation():\n",
    "    # input 은 없으며 그저 crossbar resistance를 plotting 해줌.\n",
    "    # 그 plotting을 에니메이션적으로 할 수 있으면 좋겠다. \n",
    "    # 그 기준축을 평균으로 잡고, scale도 분산을 계산해서 plotting 되는 것 같은데 그렇게 되지 않도록 하기"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def split_train_test(img_path, train_test_ratio):\n",
    "    # img path 를 받아서 train, test set을 list로 추출해서 나눠줌\n",
    "    return train_path_list, test_path_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# generator for train_img\n",
    "def crossbar_input_list(img_path_list):\n",
    "    # size 변경해서 yield 하기\n",
    "    yield crossbar_input_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def converted_img_list(img_path_list):\n",
    "    # convert or transpose\n",
    "    yield converted_input_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def after_imaging(img, label, time):\n",
    "    # ms 단위의 이미지 sending (그 때 써놓았던 방식대로 작동될 수 있도록 하기)\n",
    "    # image의 검은 부분은 -V 로 들어갈 수 있도록 만들기\n",
    "    # generator 을 사용해서 하나씩 넣는 method 를 만들어도 괜찮을것같다. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mem_output(img):\n",
    "    # 현재 memristor 상태에서 input 넣고 테스트하기\n",
    "    return crossbar_output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loss_function(img_list, label_list):\n",
    "    # label list와 비교해서하는 loss function\n",
    "    # 이 때는 softmax을 적용해서 계산해도 될것같다.\n",
    "    return loss_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def main_training(img_list):\n",
    "    # img list를 넣어서 각각 after_imaging을 시켜줌.\n",
    "    # 알고리즘적으로 5번마다 loss check함. test_img 에 대해서 loss check도 해줌\n",
    "    # loss function 구현\n",
    "    # returning accuracy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Testing with image input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def predict(crossbar_output):\n",
    "    # winner take all method\n",
    "    return prediction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
